package demolinkedlist;

/**
 * @Description
 */

public class MyLinkedList {
    static class ListNode{
        int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    public boolean contains(int key){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur!=null){
            count++;
            cur = cur.next;

        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;

        }
        System.out.println();
    }
    public void addFirst(int data){
        ListNode Node = new ListNode(data);
        if(head==null){
            head = last = Node;
            return ;//如果不返回那么要else
        }
        Node.next = head;
        head.prev = Node;
        head = Node;
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        int count = 0;
        ListNode cur = head;
        while(count!=(size()-1)){
            cur = cur.next;
            count++;
        }
        last = cur;
       /* if(last==null){
            last = head = node;
            return;
        }*/
        last.next = node;
        node.prev = last;
        last = node;
    }
    public void addIndex(int Index){
        if(Index<0||Index>size()){
            return;
        }
        ListNode node = new ListNode(100);
        ListNode cur = findIndex(Index);
        //先改尾巴再换头，防止指向丢失
        node.next = cur;
        cur.prev.next = node;
        node.prev= cur.prev;
        cur.prev = node;


    }
    public ListNode findIndex(int Index){
        ListNode cur = head;
        int count = 0;
        while(count!=Index){
            cur = cur.next;
            count++;
        }
        return cur;
    }
   /* public void show(){
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }*/
    public void remove(int key){
        ListNode cur = head;
        while(cur!=null) {
            if (cur.val == key) {
                //cur为head的时候
                if (cur == head) {
                    //当只有一个元素的时候，head=last=cur
                    head = head.next;
                    if(head!=null){
                        head.prev = null;
                    }else{
                        last = null;
                    }
                    /*head.next.prev = null;
                    head = head.next;*/
                    return;
                }
                //cur是尾巴的时候
                if (cur.next == null) {
                    cur.prev.next = null;
                    last = cur.prev;
                    return;
                }
                //cur在中间的时候
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            } else
                cur = cur.next;
        }
    }
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur!=null) {
            if (cur.val == key) {
                //cur为head的时候
                if (cur == head) {
                    //当只有一个元素的时候，head=last=cur
                    head = head.next;
                    if(head!=null){
                        head.prev = null;
                    }else{
                        last = null;
                    }
                    /*head.next.prev = null;
                    head = head.next;*/

                }
                //cur是尾巴的时候
                else if (cur.next == null) {
                    cur.prev.next = null;
                    last = cur.prev;

                }
                //cur在中间的时候
                else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }

            }
                cur = cur.next;
        }
    }
    public void clear(){
        ListNode cur = head;
        while(cur!=null){
            if(head==null){
                return;
            }
            ListNode curN = cur.next;
            cur.next = null;
            cur.prev = null;
            //如果是cur = null那么next和prev还都在，后面的display还是能够遍历
            //cur = null;

            //这里，到3的时候执行完这一行cur=null，那么在display里还是会执行next
            cur = curN;
        }
        System.out.println("-------");
    }

}
